from flask import Flask, render_template, session, redirect, url_for, flash
from flask_bootstrap import Bootstrap
from flask_script import Manager
from flask_moment import Moment
from datetime import datetime
#from flask_wtf import Form
#from wtforms import StringField, SubmitField
#from wtforms.validators import Required
from flask_sqlalchemy import SQLAlchemy
from flask_mail import Mail
from config import config
from flask_login import LoginManager
from flask_pagedown import PageDown

login_manager = LoginManager()
login_manager.session_protection = 'strong'
login_manager.login_view = 'auth.login'

bootstrap = Bootstrap()
mail = Mail()
moment = Moment()
db = SQLAlchemy()
#manager = Manager()
pagedown = PageDown()

def create_app(config_name):
    app = Flask(__name__)
    app.config.from_object(config[config_name])
    config[config_name].init_app(app)

    bootstrap.init_app(app)
    mail.init_app(app)
    moment.init_app(app)
    #
    pagedown.init_app(app)
    #
    db.init_app(app)
    # can add route and self define error html
    from .main import main as main_blueprint
    app.register_blueprint(main_blueprint)
    #
    from .auth import auth as auth_blueprint
    app.register_blueprint(auth_blueprint, url_prefix='/auth')
    #
    #
    from .api_1_0 import api as api_1_0_blueprint
    app.register_blueprint(api_1_0_blueprint,url_prefix='/api/v1.0')
    #
    login_manager.init_app(app)
    return app
#app = Flask(__name__)
#app.config['SECRET_KEY']= 'hard to guess string'
#app.config['SQLALCHEMY_DATABASE_URI']='mysql://root:admin!01@127.0.0.1/FLASK_WEB'
#app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN'] = True
#app.config['MAIL_SERVER'] = 'smtp.qq.com'
#app.config['MAIL_PORT'] = 465
#app.config['MAIL_USE_TLS'] = True
#app.config['MAIL_USE_SSL'] = True
#app.config['MAIL_USERNAME'] = '3100865876'
#app.config['MAIL_PASSWORD'] = 'jbelehqmjlxydcdd'
#app.config['FLASK_ADMIN'] = 'cjd9023@qq.com'
#manager = Manager(app)
#bootstrap = Bootstrap(app)
#moment = Moment(app)
#db = SQLAlchemy(app)
#mail = Mail(app)

#from flaskweb.model.model import User, Role



#from flask_script import Shell

#def make_shell_context():
#    return dict(app=app, db=db, User=User, Role=Role)
#manager.add_command("shell", Shell(make_context=make_shell_context))

#from flask_migrate import Migrate, MigrateCommand

#migrate = Migrate(app, db)
#manager.add_command('db', MigrateCommand)

#from flask_mail import Message
#app.config['FLASK_MAIL_SUBJECT_PREFIX'] = '[Flasky]'
#app.config['FLASK_MAIL_SENDER'] = 'Flasky Admin <cjd7749@qq.com>'



#from controller import ctr